package com.hs.hlw.credit.system.web.controller.api;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.dto.SysConfigDto;
import com.hs.hlw.credit.system.business.service.SysConfigService;
import com.hs.hlw.credit.system.common.util.BeanUtil;
import com.hs.hlw.credit.system.common.util.JsonUtil;
import com.hs.hlw.credit.system.data.DataUtils;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import com.hs.hlw.credit.system.data.primary.model.SysConfig;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * SysConfigController
 * @author zhb
 * @date 2018/10/29
 */
@RestController
@RequestMapping("/api/sys/config")
public class SysConfigController {
    private @Autowired SysConfigService sysConfigService;

    @GetMapping(path = "page")
    @ApiOperation(value = "分页查询系统参数", notes = "分页查询系统参数")
    public ResponseDto page(@RequestParam(required = false) @ApiParam(value = "英文名称") String englishName,
                            @RequestParam(required = false) @ApiParam(value = "中文名称") String chineseName,
                            @RequestParam(required = false) @ApiParam(value = "类型") Short type,
                            @RequestParam(required = false) @ApiParam(value = "状态(0:无效，1:有效)") Short status,
                            @RequestParam(required = false, defaultValue = "1") @ApiParam(value = "查询页码", defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "10") @ApiParam(value = "每页数据条数", required = true, defaultValue = "10") Integer size,
                            @RequestParam(required = false, defaultValue = "id") @ApiParam(value = "排序字段（多个用逗号隔开）", defaultValue = "id") String orderField,
                            @RequestParam(required = false, defaultValue = "asc") @ApiParam(value = "排序规则（asc:升序，desc:降序，多个用逗号隔开）", defaultValue = "asc") String direction) {

        Pageable pageable = DataUtils.pageable(page, size, orderField, direction);
        pageable.addParam("englishName", englishName);
        pageable.addParam("chineseName", chineseName);
        pageable.addParam("type", type);
        pageable.addParam("status", status);
        return sysConfigService.page(pageable);
    }

    @PostMapping(path = "add")
    @ApiOperation(value = "新增系统参数", notes = "新增系统参数")
    public ResponseDto add(@RequestBody @Valid SysConfigDto.Add data) {
        return sysConfigService.add(BeanUtil.chang(data, SysConfig.class));
    }

    @PostMapping(path = "update")
    @ApiOperation(value = "修改系统参数", notes = "修改系统参数")
    public ResponseDto add(@RequestBody @Valid SysConfigDto.Update data) {
        return sysConfigService.update(BeanUtil.chang(data, SysConfig.class));
    }

    @PostMapping(path = "delete")
    @ApiOperation(value = "删除系统参数", notes = "删除系统参数")
    public ResponseDto delete(@RequestParam @ApiParam(value = "id列表(使用json数据，包含的属性有: [id])", required = true) String list) {
        List<SysConfig> _list = JsonUtil.fromJsonArray(list, SysConfig.class);
        return sysConfigService.delete(_list);
    }

    @PostMapping(path = "updateStatus")
    @ApiOperation(value = "更新系统参数状态", notes = "更新系统参数状态")
    public ResponseDto updateStatus(@RequestParam @ApiParam(value = "id列表(使用json数据，包含的属性有: [id, status])", required = true) String list) {
        List<SysConfig> _list = JsonUtil.fromJsonArray(list, SysConfig.class);
        return sysConfigService.updateStatus(_list);
    }

    @GetMapping(path = "detail")
    @ApiOperation(value = "系统参数详情", notes = "系统参数详情")
    public ResponseDto detail(@RequestParam @ApiParam(value = "id", required = true) Integer id) {
        return sysConfigService.detail(id);
    }
}